package app.models.booking;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  餐饮日预订状态
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-05-16 23:05
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_dining_dialy_stat")
public class DiningDialyStat extends Model<DiningDialyStat> {

    public static final DiningDialyStat dao = new DiningDialyStat();
    private static final long serialVersionUID = 3696500631863894158L;

    /**
     * 根据品牌桌位日期市别查询
     *
     * @param brandId  品牌
     * @param table_category 桌位
     * @param date 日期
     * @param category 市别
     * @return 统计记录
     */
    public DiningDialyStat findByBrandTableDialyCategory(long brandId, int table_category, Date date, String category) {
        return findFirst(SqlKit.sql("diningdialystat.findByBrandTableDialyCategory"), brandId, table_category, date, category);
    }

    /**
     * 根据品牌桌位日期查询
     *
     * @param brandId  品牌
     * @param table_category 桌位
     * @param date 日期
     * @return 统计记录
     */
    public DiningDialyStat findByBrandTableDialy(long brandId, int table_category, Date date) {
        return findFirst(SqlKit.sql("diningdialystat.findByBrandTableDialy"), brandId, table_category, date);
    }

    public Page<DiningDialyStat> datatable(DatatablesCriterias criterias, String brand, String start_date, String end_date) {
        StringBuilder sb = new StringBuilder(" WHERE 1=1 ");
        ArrayList param = Lists.newArrayList();

        if(!Strings.isNullOrEmpty(start_date)){
            sb.append(" AND stat.stat_date >=?");
            param.add(start_date);
        }

        if (!Strings.isNullOrEmpty(end_date)){
            sb.append(" AND stat.stat_date <= ?");
            param.add(end_date);
        }

        if(!Strings.isNullOrEmpty(brand)){
            sb.append(" AND stat.brand=?");
            param.add(brand);
        }

        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();

        String where = SqlKit.sql("diningdialystat.columns-where") + sb.toString();

        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY stat.id DESC";
        }

        if(param.size() > 0){
            return paginate(start, pageSize, SqlKit.sql("diningdialystat.columns-select"), where,param.toArray());
        }else{
            return paginate(start, pageSize, SqlKit.sql("diningdialystat.columns-select"), where);
        }

    }
}

